package com.rd.student.dashuju.wangbo.examination;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;

public class Team {

    public static void main(String[] args) throws Exception{
        HashMap<String,String> map =new HashMap<>();
        File file = new File("player.txt");
        Writer bw = new BufferedWriter(new FileWriter(file));
        System.out.println("1、录入球员信息");
        System.out.println("2、查找球员信息");
        System.out.println("3、退出");
        Scanner scanner = new Scanner(System.in);
        while (true){
            switch (scanner.nextInt()){
                case 1:
                    System.out.println("录入球员个数：");
                    int n = scanner.nextInt();
                    for (int i = 0; i < n; i++) {
                        map = entering(map,bw);
                    }
                    bw.close();
                    System.out.println("1、录入球员信息");
                    System.out.println("2、查找球员信息");
                    System.out.println("3、退出");
                    continue;
                case 2:
                    System.out.println("请输入查找球员姓名：");
                    scanner.nextLine();
                    find(scanner.nextLine(),map);
                    System.out.println("1、录入球员信息");
                    System.out.println("2、查找球员信息");
                    System.out.println("3、退出");
                    continue;
                case 3:
                    break;
                default:
                    System.out.println("输入有误");
            }
        }
    }

    public static HashMap<String,String> entering (HashMap<String,String> map , Writer bw) throws Exception{
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入球员姓名：");
        String name = scanner.nextLine();
        System.out.println("请输入球员球队：");
        String team = scanner.nextLine();
        map.put(name,team);
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            String value = map.get(key);
            System.out.println("key = " + key + ", value = " + value);
            String s = key + "," + value;
            bw.write(s);
        }
        return map;
    }

    public static void find(String name , HashMap<String,String> map){
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            if (key.equals(name)){
                String value = map.get(key);
                System.out.println("key = " + key + ", value = " + value);
            }else {
                System.out.println("您查找的球员不存在");
            }
        }
    }
}
